package Leetcode.array;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Intersect {
    public int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> in=new LinkedList<>();

        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int i=0;
        int j=0;
        while(i<nums1.length &&j<nums2.length ){
            if (nums1[i]==nums2[j]){
                in.add(nums1[i]);
                i++;
                j++;
            }else if(nums1[i]<nums2[j]){
                i++;
            }else
                j++;
        }

        int[] result=new int[in.size()];
        for(int m=0;m<in.size();m++){
            result[m]=in.get(m);
        }
       return result;
    }

    public static void main(String[] args) {
     int[] a={1,2,5,1};
     int[] b={2,5,1,0,4};
     Intersect intersect=new Intersect();
     int[] count =intersect.intersect(a,b);
          for (int j=0;j<count.length;j++)
              System.out.println(count[j]);

    }
}
